<!DOCTYPE html>
<meta charset="utf-8"/>
<title>WebSocket Test</title>
<!--<script src="/static/sockjs.min.js"/>-->
<!--<script src="/static/stomp.min.js"/>-->
<script language="javascript" type="text/javascript">

    var wsUri = "ws://localhost:3838/webSocket";
    var output;

    function init() {
        output = document.getElementById("output");
    }

    function testWebSocket() {
        writeToScreen("-------------------------------------------------------");
        writeToScreen("连接到：" + wsUri + document.getElementById('i0').value);
        websocket = new WebSocket(wsUri + document.getElementById('i0').value);
        //var sockjs = new SockJS("http://192.168.20.128:8081/server");
        //var stompCilent = Stomp.over(sockjs);
        //stompCilent.send("/server",{},"sockjs");

        websocket.onopen = function (evt) {
            onOpen(evt)
        };
        websocket.onclose = function (evt) {
            onClose(evt)
        };
        websocket.onmessage = function (evt) {
            onMessage(evt)
        };
        websocket.onerror = function (evt) {
            onError(evt)
        };
    }

    function onOpen(evt) {
        writeToScreen("CONNECTED");
    }

    function onClose(evt) {
        writeToScreen("DISCONNECTED");
    }

    function onMessage(evt) {
        writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
    }

    function onError(evt) {
        writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
    }

    function doSend(message) {
        writeToScreen("SENT: " + message);
        websocket.send(message);
    }

    function writeToScreen(message) {
        var pre = document.createElement("p");
        pre.style.wordWrap = "break-word";
        pre.innerHTML = message;
        output.appendChild(pre);
    };

    function clearScreen() {
        output.innerHTML = '';
    }

    function sendMessage(code) {
        var message = "{'code':'" + code + "','params':{'pa1':" + i1.value + ",'pa2':" + i2.value + ",'pa3':" + i3.value + ",'pa4':" + i4.value + "}}";
        doSend(message);
    }

    window.addEventListener("load", init, false);
</script>
<body>
<h2>WebSocket Test</h2>
<div id="output"></div>
<div>
    <input id="i0"/>
    <button id="b0" onclick="javascript:testWebSocket()">点击连接</button>
    <input id="i1"/>
    <label id="l1">pa1</label>
    <input id="i2"/>
    <label id="l2">pa2</label>
    <input id="i3"/>
    <label id="l3">pa3</label>
    <input id="i4"/>
    <label id="l4">pa4</label>
    <button id="b1" onclick="javascript:sendMessage('str')">str发送</button>
    <button id="b3" onclick="javascript:sendMessage('int')">int发送</button>
    <button id="b2" onclick="javascript:clearScreen()">清空消息</button>

</div>
</body>
</html>